import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { EduSchoolContactApi } from '#/api/edu/base/schoolContact';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { Typography } from 'ant-design-vue';

import { getSchoolPageSimple } from '#/api/edu/base/school';
import { getAdminUserPageSimplePermission } from '#/api/system/user';
import { DictTag } from '#/components/dict-tag';
import { getRangePickerDefaultProps } from '#/utils';

const { Paragraph } = Typography;
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'schoolId',
      label: '学校',
      rules: 'required',
      component: 'ApiPagingSelect',
      componentProps: {
        api: getSchoolPageSimple,
        labelField: 'schoolName',
        valueField: 'id',
        placeholder: '请选择学校',
      },
    },
    {
      fieldName: 'contactType',
      label: '负责人类型',
      rules: 'required',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.SCHOOL_CONTACT_TYPE),
        placeholder: '请选择负责人类型',
      },
    },
    {
      fieldName: 'contactUserId',
      label: '联系人对应的用户',
      component: 'ApiPagingSelect',
      componentProps(values) {
        return {
          api: getAdminUserPageSimplePermission,
          labelField: 'text',
          valueField: 'id',
          placeholder: '请选择联系人对应的用户',
          onChange: (val, option: any) => {
            if (val) {
              values.contactName = option.fullName || option.nickname;
              values.contactPhone = option.mobile;
              values.contactIdcard = option.idcard;
            }
          },
        };
      },
    },
    {
      fieldName: 'contactName',
      label: '联系人姓名',
      component: 'Input',
      rules: 'required',
      componentProps: {
        placeholder: '请输入联系人姓名',
      },
    },
    {
      fieldName: 'contactPhone',
      label: '联系手机',
      rules: 'mobile',
      component: 'Input',
      componentProps: {
        placeholder: '请输入联系手机',
      },
    },
    {
      fieldName: 'contactIdcard',
      label: '身份证号',
      rules: 'idcard',
      component: 'Input',
      componentProps: {
        placeholder: '请输入身份证号',
      },
    },
    {
      fieldName: 'contactEmail',
      label: '邮箱',
      component: 'Input',
      rules: 'email',
      componentProps: {
        placeholder: '请输入邮箱',
      },
    },
    {
      fieldName: 'contactTelphone',
      label: '联系固定电话',
      component: 'Input',
      componentProps: {
        placeholder: '请输入联系固定电话',
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        rows: 4,
        placeholder: '请输入备注',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '学校',
      field: 'schoolName',
    },
    {
      label: '联系人类型',
      field: 'contactType',
      render: (val) => {
        return h(DictTag, {
          type: DICT_TYPE.SCHOOL_CONTACT_TYPE,
          value: val,
        });
      },
    },

    {
      label: '联系人姓名',
      field: 'contactName',
    },
    {
      label: '联系手机',
      field: 'contactPhone',
    },
    {
      label: '身份证号',
      field: 'contactIdcard',
    },
    {
      label: '邮箱',
      field: 'contactEmail',
    },
    {
      label: '联系固定电话',
      field: 'contactTelphone',
    },
    {
      label: '系统用户id',
      field: 'contactUserId',
    },
    {
      label: '备注',
      field: 'remark',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'schoolId',
      label: '学校',
      component: 'ApiPagingSelect',
      componentProps: {
        api: getSchoolPageSimple,
        labelField: 'schoolName',
        valueField: 'id',
        placeholder: '请选择学校',
      },
    },
    {
      fieldName: 'contactType',
      label: '负责人类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.SCHOOL_CONTACT_TYPE),
        placeholder: '请选择负责人类型',
      },
    },
    {
      fieldName: 'contactUserId',
      label: '所属用户',
      component: 'ApiPagingSelect',
      componentProps: {
        api: getAdminUserPageSimplePermission,
        labelField: 'text',
        valueField: 'id',
        placeholder: '请选择联系人对应的用户',
        allowClear: true,
      },
    },
    {
      fieldName: 'contactName',
      label: '联系人姓名',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入联系人姓名（不支持模糊搜索）',
      },
    },
    {
      fieldName: 'contactPhone',
      label: '联系手机',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入联系手机（不支持模糊搜索）',
      },
    },
    {
      fieldName: 'contactIdcard',
      label: '身份证号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入身份证号（不支持模糊搜索）',
      },
    },
    {
      fieldName: 'contactTelphone',
      label: '联系固定电话',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入联系固定电话（不支持模糊搜索）',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<EduSchoolContactApi.SchoolContact>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '主键',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'schoolId',
      title: '学校id',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'schoolName',
      title: '学校名称',
      minWidth: 200,
    },
    {
      field: 'contactType',
      title: '负责人类型',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.SCHOOL_CONTACT_TYPE },
      },
    },

    {
      field: 'contactName',
      title: '联系人姓名',
      minWidth: 120,
    },
    {
      field: 'contactPhone',
      title: '联系手机',
      minWidth: 120,
    },
    {
      field: 'contactIdcard',
      title: '身份证号',
      minWidth: 150,
    },
    {
      field: 'contactEmail',
      title: '邮箱',
      minWidth: 150,
    },
    {
      field: 'contactTelphone',
      title: '联系固定电话',
      minWidth: 120,
    },
    {
      field: 'remark',
      title: '备注',
      minWidth: 300,
    },
    {
      field: 'contactUserId',
      title: '负责人对应的用户id',
      minWidth: 120,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
